﻿using System;
using HighDiveEngine.Aggregates;
using HighDiveEngine.Resources;

namespace HighDiveEngine.Model
{
    /// <summary>
    ///Class for contest management
    /// </summary>
    public class Contest
    {
        private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        #region Properties

        /// <summary>
        /// DatabaseId
        /// </summary>
        public int? DbId { get; set; }
        public string Name { get; set; }
        public string Date { get; set; }
        public string Club { get; set; }
        public string City { get; set; }
        public string Country { get; set; }

        /// <summary>
        /// ContestStatus determines whether the competition is Unstarted / Started / Finished
        /// </summary>
        public ContestStatus Status { get; set; }

        /// <summary>
        /// List of the contest judges
        /// </summary>
        public JudgeList JudgeList { get; set; }

        /// <summary>
        /// Queue of future jumpers. This list is empty when the contest ended.
        /// </summary>
        public DiveList DiveQueue { get; set; }

        /// <summary>
        /// History of the performed jumps. This list is full when the contest ended.
        /// </summary>
        public DiveList DiveHistory { get; set; }

        #endregion

        #region Public methods

        /// <summary>
        /// Add a new convictions or points 
        /// update an already set points
        /// Check if JudgeScore list is complete, if so then               
        /// call TotalScore().
        /// </summary>
        public void UpdateDiveScore(DiveScore score, Dive dive)
        {            
            if (Log.IsDebugEnabled)
            {
                Log.Debug("Dive " + dive.QueueOrderNumber + " score updated by " + score.Judge.IdString + " to " + score.Score);
            }

            if (!dive.DiveScoreList.Exists(x => x.Judge.IdString == score.Judge.IdString))
            {
                dive.DiveScoreList.Add(score);
            }
            else
            {
                dive.DiveScoreList.Find(x => x.Judge.IdString == score.Judge.IdString).Score = score.Score;
            }

            Log.Info("Dive " + dive.QueueOrderNumber + " score updated by " + score.Judge.IdString + " to " + score.Score);            
        }

        /// <summary>
        /// Moves the first dive in the dive queue into the dive history.
        /// </summary>
        public void UpdateDiveQueue()
        {
            if (Log.IsDebugEnabled)
            {
                Log.Debug("Dive " + DiveQueue[0].QueueOrderNumber + " (DbId:" + DiveQueue[0].DbId + ") moved from queue to history"); 
            }
            DiveQueue[0].Date = DateTime.Now.Year.ToString("0000") + "-" + DateTime.Now.Month.ToString("00") + "-" + DateTime.Now.Day.ToString("00") + "-" +
                                  DateTime.Now.Hour.ToString("00") + ":" + DateTime.Now.Minute.ToString("00");
            DiveHistory.Insert(0, (DiveQueue[0]));
            DiveQueue.RemoveAt(0);
        }

        /// <summary>
        /// Checks that all judge scores have been entered for a dive
        /// </summary>
        /// <param name="dive">The dive which to check</param>
        /// <returns>true if all scores have been entered, otherwise false</returns>
        public bool CheckScoreComplete(Dive dive)
        {
            return dive.DiveScoreList.Count == JudgeList.NumberOfJudges;
        }

        public override string ToString()
        {
            return Name + ": " + Date.Insert(13, ":") + " | " + Club;
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Creates a new contest.
        /// </summary>
        public Contest()
        {
            Status = ContestStatus.Unstarted;
            DbId = null;
            Name = null;
            Date = null;
            Club = null;
            City = null;
            Country = null;
            DiveHistory = null;
            DiveQueue = null;
            JudgeList = null;
        }

        #endregion
    }
}
